var express = require("express");
var router = express.Router();
var utils = require('../utils/index');
var mysql = require("../connect/index");
var { Sales, SalesFlow } = require('../models/shop/sales.model');

router.get("/", function (req, res, next) {
    res.send(utils.setSendResult());
});


// 获取售后信息
router.get("/list", function (req, res, next) {
    const query = req.query;
    Sales.findAll().then(result => {
        res.send(utils.setSendResult(200, "查询成功", result));
    })
});
// 新增售后信息
router.post("/add", function (req, res, next) {
    const params = req.body;
    var sales = Sales.create(params);
    res.send(utils.setSendResult(200, "添加成功", sales));
});
// 批量新增售后表 - 执行 sql 语句
router.get("/mock/sql", async (req, res, next) => {
    // 假设有两个表的数据
    const shop_sales = [
        { orderid: '11292883842', desc: '采购10个产品', thumb: 'https://img1.baidu.com/it/u=2348129149,2334837453&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500', carryid: '61292883842', state: '0'},
        { orderid: '11292883843', desc: '采购20个产品', thumb: 'https://img1.baidu.com/it/u=2348129149,2334837453&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500', carryid: '61292883843', state: '1'},
        { orderid: '11292883844', desc: '采购30个产品', thumb: 'https://img1.baidu.com/it/u=2348129149,2334837453&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500', carryid: '61292883844', state: '2'},
        { orderid: '11292883845', desc: '采购40个产品', thumb: 'https://img1.baidu.com/it/u=2348129149,2334837453&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500', carryid: '61292883845', state: '3'},
        { orderid: '11292883846', desc: '采购50个产品', thumb: 'https://img1.baidu.com/it/u=2348129149,2334837453&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500', carryid: '61292883846', state: '0'},
        { orderid: '11292883847', desc: '采购60个产品', thumb: 'https://img1.baidu.com/it/u=2348129149,2334837453&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500', carryid: '61292883847', state: '1'},
        // 更多行...
    ];
    const shop_sales_flow = [{
        salesid: 1, prestate: 1, state: 1, operator: '王晓华', remark: '备注信息'
    }, {
        salesid: 2, prestate: 2, state: 2, operator: '李晓明', remark: '备注信息'
    }, {
        salesid: 3, prestate: 2, state: 1, operator: '张小虎', remark: '备注信息'
    }, {
        salesid: 4, prestate: 1, state: 2, operator: '赵得住', remark: '备注信息'
    }, {
        salesid: 5, prestate: 3, state: 2, operator: '王晓华', remark: '备注信息'
    }, {
        salesid: 6, prestate: 1, state: 1, operator: '张小虎', remark: '备注信息'
    }]

    try {
        await mysql.sequelize.sync(); // 同步模型到数据库
        // 批量插入数据1
        const insertSales = await Sales.bulkCreate(shop_sales);
        // 批量插入数据2
        const insertSalesFlow = await SalesFlow.bulkCreate(shop_sales_flow);
        res.send(utils.setSendResult(200, "批量添加成功", insertSales,insertSalesFlow));
    } catch (error) {
        res.send(utils.setSendResult(500,  error));
    }
})
// 修改售后信息
router.post("/edit", function (req, res, next) {
    const params = req.body;
    if (params['id'] == undefined) return res.send(utils.setSendResult(400, "参数错误", {}));
    var sales = Sales.update(params);
    res.send(utils.setSendResult(200, "修改成功", sales));
});
// 删除售后信息
router.get("/delete", function (req, res, next) {
    const query = req.query;
    if (query['id'] == undefined) return res.send(utils.setSendResult(400, "参数错误", {}));
    var sales = Sales.destroy({
        where: {
            id: query['id']
        }
    });
    res.send(utils.setSendResult(200, "删除成功", sales));
});

module.exports = router;
